﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_DangKyTiem : System.Web.UI.Page
{
    TiemPhongDataContext db = new TiemPhongDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDataListView();
        }
    }
    // hàm loadDataListView
    protected void loadDataListView()
    {
        var l = db.DangKyTiems.ToList();
        this.ListViewDangKyTiem.DataSource = l;
        this.ListViewDangKyTiem.DataBind();
        txtMDK.Enabled = false;
        txtMDK.Text = loadMDK();
    }
    // hàm tính txtMDK
    protected string loadMDK()
    {
        string countID = "";
        var selectID = (from u in db.DangKyTiems select u.MaDK).Count() + 1;
        countID = selectID.ToString();
        countID = "DKT" + countID;
        return countID;
    }
    // hàm set lại TextBox
    protected void ClearControl()
    {
        txtMDK.Text = string.Empty;
        txtCMTBoMe.Text = string.Empty;
        txtTenNguoiDK.Text = string.Empty;
        txtTenBe.Text = string.Empty;
        txtNgaySinh.Text = string.Empty;
        txtTuoi.Text = string.Empty;        
        txtDiaChi.Text = string.Empty;
        txtNgayDangKy.Text = string.Empty;
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        try
        {
            // khoi tao doi tuong DangKyTiem moi
            DangKyTiem dkt = new DangKyTiem();
            // gan gia tri
            dkt.MaDK = txtMDK.Text;
            dkt.CMTBoMe = Convert.ToInt32(txtCMTBoMe.Text);
            dkt.TenNguoiDK = txtTenNguoiDK.Text;
            dkt.TenBe = txtTenBe.Text;
            dkt.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
            dkt.Tuoi = txtTuoi.Text;
            dkt.GioiTinh = Convert.ToBoolean(rbtnGioiTinh.SelectedValue);
            dkt.DiaChi = txtDiaChi.Text;
            dkt.NgayDK = Convert.ToDateTime(txtNgayDangKy.Text);
            db.DangKyTiems.InsertOnSubmit(dkt);
            db.SubmitChanges();
            // load lai listView
            loadDataListView();
            ClearControl();
            // thong bao
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Thêm thành công!";
        }
        catch (Exception ex)
        {
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Lỗi" + ex.Message;
        }
    }
    // hàm sửa
    protected void btnSua_Click(object sender, EventArgs e)
    {
        try
        {
            var kq = db.DangKyTiems.Where(k => k.MaDK == txtMDK.Text).SingleOrDefault();
            if (kq != null)
            {
                kq.CMTBoMe = Convert.ToInt32(txtCMTBoMe.Text);
                kq.TenNguoiDK = txtTenNguoiDK.Text;
                kq.TenBe=txtTenBe.Text;
                kq.NgaySinh = DateTime.ParseExact(this.txtNgaySinh.Text, "dd/MM/yy",null);
                kq.Tuoi = txtTuoi.Text;
                kq.GioiTinh = Boolean.Parse(this.rbtnGioiTinh.SelectedValue);
                kq.DiaChi = txtDiaChi.Text;
                kq.NgayDK = DateTime.ParseExact(this.txtNgayDangKy.Text,"dd/MM/yy",null);
                db.SubmitChanges();
                // thông báo
                messageKetQua.Enabled=true;
                messageKetQua.Text="Sửa thành công!";
                // tải lại listView
                loadDataListView();
                ClearControl();
            }
        }
        catch(Exception xxx)
        {
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Sua that bai " + xxx.Message;
        }
    }
    ////////////
    protected void ListViewDangKyTiem_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {
        // khai bao bien de lay maDK
        var _maDK = e.CommandArgument.ToString();
        if (e.CommandName == "Delete")
        {
            var delete_maDK = db.DangKyTiems.FirstOrDefault(c => c.MaDK.Equals(_maDK));
            db.DangKyTiems.DeleteOnSubmit(delete_maDK);
            db.SubmitChanges();
            // thong bao
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Xoa ban ghi thanh cong";
            // tai lai listView
            loadDataListView();
            ClearControl();
        }
        if (e.CommandName == "Update")
        {
            var update_maDK = db.DangKyTiems.FirstOrDefault(c => c.MaDK.Equals(_maDK));
            txtCMTBoMe.Text = update_maDK.CMTBoMe.ToString();
            txtTenNguoiDK.Text = update_maDK.TenNguoiDK.ToString();
            txtTenBe.Text = update_maDK.TenBe.ToString();
            txtNgaySinh.Text = update_maDK.NgaySinh.ToString();
            txtTuoi.Text = update_maDK.Tuoi.ToString();
            // gioi tinh
            txtDiaChi.Text = update_maDK.DiaChi;
            txtNgayDangKy.Text = update_maDK.NgayDK.ToString();
        }
    }
    protected void ListViewDangKyTiem_OnItemDeleting(object sender, ListViewDeleteEventArgs e) { }
    protected void ListViewDangKyTiem_OnItemUpdating(object sender, ListViewUpdateEventArgs e) { }
}